/*头文件声明区*/
#include <STC15F2K60S2.H>
#include <Key.H>
#include <Seg.H>
#include <Led.H>
#include <init.h>
/*变量声明区域*/
unsigned char ksd;
unsigned int ssd;
unsigned char Seg_Point[8]={0,0,0,0,0,0,0,0};
unsigned char Seg_Pos;
unsigned char Seg_Buf[8]={10,10,10,10,10,10,10,10};
unsigned char ucLed[8]={0,0,0,0,0,0,0,0};
unsigned int time_500ms;
unsigned int time_1000ms;
unsigned char time[]={22,30,0};
unsigned char time_set[3];
unsigned char Seg_Mode;//0为时间显示，1为时间设置，2为闹钟设置
unsigned char time_point;
unsigned char clock[3];


bit L;//控制Led闪烁
/*按键处理函数*/
void Key_Proc()
{
	static unsigned char kv,kd,ku,ko;
	if(ksd!=0) return;
	ksd=1;
	kv=Key_Read();
	kd=kv&(kv^ko);
	ku=~kv&(kv^ko);
	ko=kv;
	
	switch(kd)
	{
		case 4:
			if(++Seg_Mode==3)Seg_Mode=0;
		    if(Seg_Mode==1)
			{
				time_set[0]=time[0];
				time_set[1]=time[1];
				time_set[2]=time[2];
			}
		break;
		case 5:
			if(Seg_Mode==1)
			{
				if(++time_point==3)time_point=0;
			}
		break;
		case 6:
			
		break;
		case 7:
			
		break;
	}
}

/*信息处理函数*/
void Seg_Proc()
{
	if(ssd!=0)return;
		ssd=1;
	switch(Seg_Mode)
	{
		case 0:
			Seg_Buf[0]=time[0]/10%10;
			Seg_Buf[1]=time[0]%10;
			Seg_Buf[2]=time[1]/10%10;
			Seg_Buf[3]=time[1]%10;
			Seg_Buf[4]=time[2]/10%10;
			Seg_Buf[5]=time[2]%10;
			Seg_Point[1]=1;
			Seg_Point[3]=1;
			Seg_Point[5]=1;
			break;
		case 1:
			Seg_Buf[0]=time_set[0]/10%10;
			Seg_Buf[1]=time_set[0]%10;
			Seg_Buf[2]=time_set[1]/10%10;
			Seg_Buf[3]=time_set[1]%10;
			Seg_Buf[4]=time_set[2]/10%10;
			Seg_Buf[5]=time_set[2]%10;
		    Seg_Buf[time_point*2]=L?(time_set[time_point]/10%10):10;
			Seg_Buf[time_point*2+1]=L?(time_set[time_point]%10):10;
			Seg_Point[1]=1;
			Seg_Point[3]=1;
			Seg_Point[5]=1;
		break;
		case 2:
			Seg_Buf[0]=time_set[0]/10%10;
			Seg_Buf[1]=time_set[0]%10;
			Seg_Buf[2]=time_set[1]/10%10;
			Seg_Buf[3]=time_set[1]%10;
			Seg_Buf[4]=time_set[2]/10%10;
			Seg_Buf[5]=time_set[2]%10;
		    Seg_Buf[time_point*2]=L?(time_set[time_point]/10%10):10;
			Seg_Buf[time_point*2+1]=L?(time_set[time_point]%10):10;
			Seg_Point[1]=1;
			Seg_Point[3]=1;
			Seg_Point[5]=1;
		break;
	}
	

}
/*其他显示函数*/
void Led_Proc()
{
	if(L) ucLed[1]=1;
	else ucLed[1]=0;
}

/*定时器0初始化函数*/
void Timer0Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
}


/*定时器0中断服务函数*/
void Timer0_Server() interrupt 1
{
	if(++ksd==10)ksd=0;
	if(++ssd==500)ssd=0;
	if(++Seg_Pos==8)Seg_Pos=0;
	Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]);
	Led_Disp(Seg_Pos,ucLed[Seg_Pos]);
	if(++time_500ms==500)
	{
		time_500ms=0;
		L^=1;
	}
	if(++time_1000ms==1000)
	{
		time_1000ms=0;
		if(++time[2]==60)
		{
			time[2]=0;
			if(++time[1]==60)
			{
				time[1]=0;
				if(++time[0]==24)
					time[0]=0;
			}
		}
		
	}
}

/*Main*/
void main()
{
	Timer0Init();
	System_Init();
	while(1)
	{
		Key_Proc();
		Seg_Proc();
		Led_Proc();
		
	}
}
